package com.example.zhangxin.myhuatuob2b.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.zhangxin.myhuatuob2b.R;
import com.example.zhangxin.myhuatuob2b.fragment.home.cart.CartFragment;
import com.example.zhangxin.myhuatuob2b.fragment.home.home.HomeFragment;
import com.example.zhangxin.myhuatuob2b.fragment.home.mine.MineFragment;
import com.example.zhangxin.myhuatuob2b.fragment.home.store.StoreFragment;

public class MainFragmentManager extends FragmentActivity implements View.OnClickListener {

    private FrameLayout fl;
    private RadioButton rbt1;
    private RadioButton rbt2;
    private RadioButton rbt3;
    private RadioButton rbt4;
    private FragmentTransaction transaction;
    private HomeFragment homeFragment;
    private CartFragment cartFragment;
    private FragmentManager manager;
    private MineFragment mineFragment;
    private StoreFragment storeFragment;
    private RadioGroup rg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fragment_manager);
        manager = getSupportFragmentManager();

        initView();

        addRbt1();
        rbt1.setChecked(true);
    }

    private void initView() {
        fl = (FrameLayout) findViewById(R.id.fl);
        rbt1 = (RadioButton) findViewById(R.id.rbt1);
        rbt2 = (RadioButton) findViewById(R.id.rbt2);
        rbt3 = (RadioButton) findViewById(R.id.rbt3);
        rbt4 = (RadioButton) findViewById(R.id.rbt4);
        rg = (RadioGroup) findViewById(R.id.rg);

        rbt1.setOnClickListener(this);
        rbt2.setOnClickListener(this);
        rbt3.setOnClickListener(this);
        rbt4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rbt1:
                addRbt1();
                break;
            case R.id.rbt2:
                addRbt2();
                break;
            case R.id.rbt3:
                addRbt3();
                break;
            case R.id.rbt4:
                addRbt4();
                break;
        }
    }

    private void hideFragment(FragmentTransaction transaction){
        if(homeFragment != null){
            transaction.hide(homeFragment);
        }
        if(cartFragment != null){
            transaction.hide(cartFragment);
        }
        if(mineFragment != null){
            transaction.hide(mineFragment);
        }
        if(storeFragment != null){
            transaction.hide(storeFragment);
        }
    }

    private void addRbt1() {
        transaction = manager.beginTransaction();
        hideFragment(transaction);
        if(homeFragment==null){
            homeFragment = new HomeFragment();
            transaction.add(R.id.fl,homeFragment);
        }else{
            transaction.show(homeFragment);
        }
        transaction.commitAllowingStateLoss();

    }
    private void addRbt2() {
        transaction = manager.beginTransaction();
        hideFragment(transaction);
        if(cartFragment==null){
            cartFragment = new CartFragment();
            transaction.add(R.id.fl,cartFragment);
        }else{
            transaction.show(cartFragment);
        }
        transaction.commitAllowingStateLoss();

    }
    private void addRbt3() {
        transaction = manager.beginTransaction();
        hideFragment(transaction);
        if(mineFragment==null){
            mineFragment = new MineFragment();
            transaction.add(R.id.fl,mineFragment);
        }else{
            transaction.show(mineFragment);
        }
        transaction.commitAllowingStateLoss();

    }
    private void addRbt4() {
        transaction = manager.beginTransaction();
        hideFragment(transaction);
        if(storeFragment==null){
            storeFragment = new StoreFragment();
            transaction.add(R.id.fl,storeFragment);
        }else{
            transaction.show(storeFragment);
        }
        transaction.commitAllowingStateLoss();

    }

}
